<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use phpDocumentor\Reflection\Types\Object_;

class AdminUserController extends Controller
{
    /**
     * 获取后台用户列表
     */
    public function index(Request $request){
        $input = $request->all();
        $keyword = $request->input('keyword');
        $list = DB::table('admin_user')
            ->when($keyword, function ($query, $keyword) {
                return $query->where(DB::raw('concat(username,name,moblie)'), 'like', '%'.$keyword.'%');
            })
            ->where('role','!=','1')
            ->paginate($input['per_page'],['*'],'page',$input['current_page']);
        self::$response['data'] = $list;
        self::$response['code'] = 1;
        return self::$response;
    }

    /**
     * 添加后台用户
     */
    public function addAdminUser(Request $request){
        $input = $request->all();
        $arr = [
            'username'=>$input['username'],
            'name'=>$input['name'],
            'moblie'=>$input['moblie'],
            'email'=>$input['email'],
            'status'=>$input['status'],
            'password'=>md5(123456),
            'role'=>2,
            'sex'=>1,
            'avatar'=>'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1937445174,2133528823&fm=27&gp=0.jpg',
        ];
        //账号是否存在
        $is_exist = DB::table('admin_user')->where('username',$input['username'])->first();
        if($is_exist){
            self::$response['msg'] = '账号已存在';
            return self::$response;
        }
        $insertId = DB::table('admin_user')->insertGetId($arr);
        if($insertId){
            self::$response['data'] = $arr;
            self::$response['code'] = 1;

        }else{
            self::$response['msg'] = '添加失败';
        }
        return self::$response;
    }

    /**
     * 获取用户详情
     */
    public function getAdminUserInfo(Request $request){
        $input = $request->all();
        $res = DB::table('admin_user')->where('id',$input['id'])->first();
        if($res){
            self::$response['data'] = $res;
            self::$response['code'] = 1;
        }else{
            self::$response['msg'] = '获取失败';

        }
        return self::$response;
    }

    /**
     * 更新用户详情
     */
    public function updateAdminUser(Request $request){
        $input = $request->all();
        $arr = [
            'name'=>$input['name'],
            'moblie'=>$input['moblie'],
            'email'=>$input['email'],
            'status'=>$input['status'],
        ];
        $res = DB::table('admin_user')->where('id',$input['id'])->update($arr);
        if($res){
            self::$response['code'] = 1;
        }else{
            self::$response['msg'] = '更新失败';
        }
        return self::$response;
    }

    /**
     * 删除后台用户
     */
    public function deleteAdminUser(Request $request){
        $input = $request->all();
        $idArr = explode(",",$input['id']);
        foreach($idArr as $v){
            $res = DB::table('admin_user')->where('id',$v)->delete();
        }
        if($res){
            self::$response['code'] = 1;
        }else{
            self::$response['msg'] = '删除失败';
        }
        return self::$response;
    }

}